var express = require('express');
var router = express.Router();
var orm = require('orm');
//DB inform
var dbusername = "root";
var dbpassword = "root";
var dbname = "bd_news";

/* GET home page. */
router.get('/', function(req, res, next) {
	res.render('admin', { title: 'keis' });
});


router.get('/admin', function(req, res, next) {
	res.render('admin', { title: 'keis' });
});



//访问 http://localhost:3000/admin/add
router.get('/add', function(req, res, next) {
	res.render('add', { title: 'keis' });
});


/* 新闻资讯 */
router.use('/news', orm.express("mysql://" + dbusername + ":" + dbpassword + "@localhost/" + dbname, {
	define: function(db, models, next) {
		models.news = db.define("news", {
			id: {
				type: 'serial',
				key: true
			},
			type_new: String,
			title: String,
			content: String,
			ctime: String,
			utime: String
		});

		next();
	}
}));

/* 获取新闻资讯 */
router.get('/news', function(req, res, next) {

	var type = req.query.type;
	var type_new="";

	if(type==0){
		type_new="新闻";
	}else if(type==1){
		type_new="军事";
	}else if(type==2){
		type_new="娱乐";
	}else{
		type_new="笑话";
	}

	req.models.news.find({ type_new:type_new},function(err, news) {

		if (err) throw err;

		var datas=new Array();
		for(var i =0; i < news.length ; i++) {

			var title=new Array()
			title['id']=news[i].id;
			title['title']=news[i].title;
			title['content']=news[i].content;
			title['type_new']=news[i].type_new;
			title['ctime']=news[i].ctime; 

			var myDate=new Date(title['ctime']);
			title['ctime']=myDate.toLocaleString();
			datas[i]=title;
		}

		res.render('news', { 
			datas: datas ,
			type:req.query.type
		});

	});


});


//添加数据
router.post('/news/add', function(req, res, next) {

	req.models.news.create({
		title: req.body.title,
		type_new: req.body.type,
		content: req.body.content,
		ctime: req.body.time,
	}, function(err, news) {
        // res.status(200);
        // res.json(news);

        console.log(news.id + ":保存成功！");
        res.status(200);
        res.send(" <script>setTimeout(function(){window.location.href='/admin';},3000);</script><h1>保存成功！</h1>");

    });
});

//删除
router.get('/news/delete', function(req, res, next) {
    // console.log(req.body.idsDel);
    var id = req.query.id;
    req.models.news.get(id, function(err, item) {
    	item.remove(function(err) {
    		console.log("删除成功！");
    		res.status(200);
    		res.send(" <script>setTimeout(function(){window.location.href='/admin';},3000);</script><h1>删除成功！</h1>");
    	});
    });
});


//编辑
router.get('/news/edit', function(req, res, next) {

    var id = req.query.id;
    req.models.news.find({ id:id },function(err, news) {

    	if (err) throw err;

    	var data=new Array()
    	data['id']=news[0].id;
    	data['title']=news[0].title;
    	data['content']=news[0].content;
    	data['type_new']=news[0].type_new;

    	var myDate=new Date(news[0].ctime);
	    data['ctime']=myDate.toLocaleString().substring(0,10);

    	res.render('edit', { 
    		data: data ,
    		type:req.query.type
    	});

    });
});


//编辑修改
router.post('/news/postedit', function(req, res, next) {

	req.models.news.get(req.body.id, function(err, item) {

		item.save({
			title: req.body.title,
			type_new: req.body.type,
			content: req.body.content,
			ctime: req.body.time,
			utime: getdate()
		}, function(err) {
			console.log(req.body.id + "保存成功！");
			res.status(200);
			res.send(" <script>setTimeout(function(){window.location.href='/admin';},3000);</script><h1>保存成功！</h1>");
		});
	});

});


//获取当前时间
function getdate()
{
	var now=new Date()
	y=now.getFullYear()
	m=now.getMonth()+1
	d=now.getDay()
	m=m<10?"0"+m:m
	d=d<10?"0"+d:d
	return y+"-"+m+"-"+d
}

module.exports = router;
